package com.xudow.app.ui.task;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.activeshare.edu.ucenter.common.messages.Message;
import com.xudow.app.Config;
import com.xudow.app.XApplication;
import com.xudow.app.model.CartModle;
import com.xudow.app.model.Coures;
import com.xudow.app.util.FileUtils;
import com.xudow.app.util.rest.XRestTemplate;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.xudow.app.util.LogUtils.LOGE;
import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 获取课程列表Task
 *
 * @author lukecheng
 */
public class ModityPwdTask extends AsyncTask<HashMap<String, String>, Void, Void> {
    private static final String TAG = makeLogTag(ModityPwdTask.class);

    private static final String MODIFY_PWD_URL = Config.BASE_URL + Config.MODIFY_PWD_URL;

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    private Context context;
    private Handler handler;

    public ModityPwdTask(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;

        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        handler.sendMessage(message);
    }

    /**
     * 更改使用message.obj传递数据
      * @param
     */
    private void sendSuccessMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;
        message.obj=msg;
        handler.sendMessage(message);
    }

    @Override
    protected Void doInBackground(HashMap<String, String>... parameters) {
      XApplication application = (XApplication) context.getApplicationContext();

        XRestTemplate restTemplate = new XRestTemplate(application.getCookieStore(), context);
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        String oldpwd=parameters[0].get("old_password");
        String newpwd=parameters[0].get("new_password");
        String phone=parameters[0].get("phone");
        MultiValueMap<String, String> valueMap = new LinkedMultiValueMap<String, String>();
        valueMap.add("new_password", newpwd);
        valueMap.add("old_password", oldpwd);
        valueMap.add("phone", phone);
        String url=MODIFY_PWD_URL;

           Message result = restTemplate.postForObject(url, valueMap, Message.class);

            if (result != null) {
                //Log.e(TAG, "doInBackground: "+result+valueMap.get("new_password")+valueMap.get("phone")+valueMap.get("old_password"));
                if (result.getResult().equals("0"))
                    sendSuccessMessage(result.getErrorMessage());
                else
                    sendErrorMessage(result.getErrorMessage());
            } else {
                sendErrorMessage(null);
            }
        return null;
    }


}
